﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;

// Token: 0x020003A0 RID: 928
public class HandleControl : MonoBehaviour
{
	// Token: 0x0600185F RID: 6239 RVA: 0x0009A120 File Offset: 0x00098320
	private void Start()
	{
		this.state = HandleControl.State.NotDrag;
		this.mainCamera = Camera.main;
		Collider[] componentsInChildren = base.GetComponentsInChildren<Collider>();
		this.xAxis = componentsInChildren[0];
		this.yAxis = componentsInChildren[1];
		this.zAxis = componentsInChildren[2];
		this.planeX = componentsInChildren[3];
		this.planeY = componentsInChildren[4];
		this.planeZ = componentsInChildren[5];
		this.DisablePlanes();
	}

	// Token: 0x06001860 RID: 6240 RVA: 0x0009A182 File Offset: 0x00098382
	private void DisablePlanes()
	{
		this.planeX.enabled = false;
		this.planeY.enabled = false;
		this.planeZ.enabled = false;
	}

	// Token: 0x06001861 RID: 6241 RVA: 0x0009A1A8 File Offset: 0x000983A8
	private void Update()
	{
		if (this.smartObject)
		{
			HandleControl.State state = this.state;
			Vector3 mousePosition = Input.mousePosition;
			HandleControl.State state2 = this.state;
			if (state2 != HandleControl.State.NotDrag)
			{
				if (state2 == HandleControl.State.Drag)
				{
					Ray ray = this.mainCamera.ScreenPointToRay(mousePosition);
					RaycastHit raycastHit;
					if (Physics.Raycast(ray, out raycastHit, float.PositiveInfinity, Layers.auxMask))
					{
						Vector3 b = Vector3.Scale(raycastHit.point - this.initialPoint, this.axis);
						base.transform.position = this.initialTransform + b;
						this.smartObject.Move(base.transform.position);
					}
					if (Input.GetMouseButtonUp(0))
					{
						this.DisablePlanes();
						state = HandleControl.State.NotDrag;
					}
				}
			}
			else
			{
				base.transform.position = this.smartObject.transform.position;
				base.transform.localScale = Vector3.one * this.smartObject.Height;
				if (!EventSystem.current.IsPointerOverGameObject() && Input.GetMouseButtonDown(0))
				{
					Ray ray = this.mainCamera.ScreenPointToRay(mousePosition);
					RaycastHit raycastHit;
					if (Physics.Raycast(ray, out raycastHit, float.PositiveInfinity, Layers.actionSelectionMask))
					{
						if (raycastHit.collider.gameObject.layer != Layers.uiLayer)
						{
							return;
						}
						if (raycastHit.collider == this.xAxis)
						{
							this.axis = Vector3.right;
						}
						else if (raycastHit.collider == this.yAxis)
						{
							this.axis = Vector3.up;
						}
						else if (raycastHit.collider == this.zAxis)
						{
							this.axis = Vector3.forward;
						}
						this.planeX.enabled = (this.axis.x == 0f);
						this.planeY.enabled = (this.axis.y == 0f);
						this.planeZ.enabled = (this.axis.z == 0f);
						this.initialTransform = base.transform.position;
						this.initialPoint = base.transform.position + Vector3.Scale(raycastHit.point - this.initialTransform, this.axis);
						state = HandleControl.State.Drag;
					}
				}
			}
			this.state = state;
		}
	}

	// Token: 0x04001A78 RID: 6776
	public EntityBase smartObject;

	// Token: 0x04001A79 RID: 6777
	private HandleControl.State state;

	// Token: 0x04001A7A RID: 6778
	private Camera mainCamera;

	// Token: 0x04001A7B RID: 6779
	private Collider xAxis;

	// Token: 0x04001A7C RID: 6780
	private Collider yAxis;

	// Token: 0x04001A7D RID: 6781
	private Collider zAxis;

	// Token: 0x04001A7E RID: 6782
	private Collider planeX;

	// Token: 0x04001A7F RID: 6783
	private Collider planeY;

	// Token: 0x04001A80 RID: 6784
	private Collider planeZ;

	// Token: 0x04001A81 RID: 6785
	private Vector3 axis;

	// Token: 0x04001A82 RID: 6786
	private Vector3 initialPoint;

	// Token: 0x04001A83 RID: 6787
	private Vector3 initialTransform;

	// Token: 0x020003A1 RID: 929
	private enum State
	{
		// Token: 0x04001A85 RID: 6789
		NotDrag,
		// Token: 0x04001A86 RID: 6790
		Drag
	}
}
